#include "action.h"
#include <QDebug>
#include <QDomDocument>
#include <QFile>
#include <QDateTime>

Action::Action()
{
}

bool Action::load(QString &xmlData)
{
    clear();
    QDomDocument domDocument;

    if (!domDocument.setContent(xmlData)) {
        return false;
    }

    QDomElement elemAction = domDocument.documentElement();
    if (0 != elemAction.tagName().compare("Action", Qt::CaseInsensitive)) {
        qDebug() << elemAction.tagName();
        return false;
    }
    qDebug() << "Action version : " << elemAction.attribute("version");
    elemAction.clear();

    QDomNodeList nodeList = domDocument.documentElement().elementsByTagName("Timer");
    for (int i=0; i<nodeList.count(); i++) {
        elemAction = nodeList.at(i).toElement();
        qDebug() << "Timer attribute cid bid rid :" << elemAction.attribute("cid") << elemAction.attribute("bid") << elemAction.attribute("rid");

        RelayTimer relayTimer;
        relayTimer.mCId = elemAction.attribute("cid").toInt();
        relayTimer.mBId = elemAction.attribute("bid").toInt();
        relayTimer.mRId = elemAction.attribute("rid").toInt();

        QDomNode node = elemAction.firstChild();
        while (false == node.isNull()) {
            QDomElement elem = node.toElement();
            qDebug() << "  " << qPrintable(elem.tagName()) << qPrintable(elem.text());

            if (0 == elem.tagName().compare("startTime", Qt::CaseInsensitive)) {
                relayTimer.mStartTime = elem.text();
            } else if (0 == elem.tagName().compare("startAction", Qt::CaseInsensitive)) {
                relayTimer.mStartAction = elem.text().toInt();
            } else if (0 == elem.tagName().compare("endTime", Qt::CaseInsensitive)) {
                relayTimer.mEndTime = elem.text();
            } else if (0 == elem.tagName().compare("endAction", Qt::CaseInsensitive)) {
                relayTimer.mEndAction = elem.text().toInt();
            } else if (0 == elem.tagName().compare("dayOfWeek", Qt::CaseInsensitive)) {
                relayTimer.mDayOfWeek = elem.text().toInt();
            }

            node = node.nextSibling();
        }

        if (TIME_SUNRISE_STR == relayTimer.mStartTime) {
            relayTimer.mStartSunLight = TIME_SUNRISE;
        } else if (TIME_SUNSET_STR == relayTimer.mStartTime) {
            relayTimer.mStartSunLight = TIME_SUNSET;
        }
        if (TIME_SUNRISE_STR == relayTimer.mEndTime) {
            relayTimer.mEndSunLight = TIME_SUNRISE;
        } else if (TIME_SUNSET_STR == relayTimer.mEndTime) {
            relayTimer.mEndSunLight = TIME_SUNSET;
        }
        mTimers.append(relayTimer);
    }
    nodeList = domDocument.documentElement().elementsByTagName("Computer");
    for (int i=0; i<nodeList.count(); i++) {
        elemAction = nodeList.at(i).toElement();
        qDebug() << "Computer attribute cid bid rid :" << elemAction.attribute("cid") << elemAction.attribute("bid") << elemAction.attribute("rid");

        RelayComputer relayComputer;
        relayComputer.mCId = elemAction.attribute("cid").toInt();
        relayComputer.mBId = elemAction.attribute("bid").toInt();
        relayComputer.mRId = elemAction.attribute("rid").toInt();

        QDomNode node = elemAction.firstChild();
        while (false == node.isNull()) {
            QDomElement elem = node.toElement();
            qDebug() << "  " << qPrintable(elem.tagName()) << qPrintable(elem.text());

            if (0 == elem.tagName().compare("name", Qt::CaseInsensitive)) {
                relayComputer.mName = elem.text();
            } else if (0 == elem.tagName().compare("account", Qt::CaseInsensitive)) {
                relayComputer.mAccount = elem.text();
            } else if (0 == elem.tagName().compare("password", Qt::CaseInsensitive)) {
                relayComputer.mPassword = elem.text();
            }

            node = node.nextSibling();
        }

        mComputers.append(relayComputer);
    }


    qDebug() << "mTimers.count() :" << mTimers.count();

    return true;
}

QString Action::save(const QString fileName)
{
    QDomDocument document;
    QString baseFormat = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
    document.setContent(baseFormat);

    QDomElement elemAction = document.createElement("Action");
    elemAction.setAttribute("version", 1);

    if (mTimers.count() | mComputers.count()) {
        QDomElement domElem;
        QDomText domText;
        for (int i=0; i<mTimers.count(); i++) {
            QDomElement elemTimer = document.createElement("Timer");
            elemTimer.setAttribute("cid", mTimers[i].mCId);
            elemTimer.setAttribute("bid", mTimers[i].mBId);
            elemTimer.setAttribute("rid", mTimers[i].mRId);

            domElem = document.createElement("startTime");
            domText = document.createTextNode(mTimers[i].mStartTime);
            domElem.appendChild(domText);
            elemTimer.appendChild(domElem);

            domElem = document.createElement("startAction");
            domText = document.createTextNode(QString("%1").arg(mTimers[i].mStartAction));
            domElem.appendChild(domText);
            elemTimer.appendChild(domElem);

            domElem = document.createElement("endTime");
            domText = document.createTextNode(mTimers[i].mEndTime);
            domElem.appendChild(domText);
            elemTimer.appendChild(domElem);

            domElem = document.createElement("endAction");
            domText = document.createTextNode(QString("%1").arg(mTimers[i].mEndAction));
            domElem.appendChild(domText);
            elemTimer.appendChild(domElem);

            domElem = document.createElement("dayOfWeek");
            domText = document.createTextNode(QString("%1").arg(mTimers[i].mDayOfWeek));
            domElem.appendChild(domText);
            elemTimer.appendChild(domElem);

            elemAction.appendChild(elemTimer);
        }
        for (int i=0; i<mComputers.count(); i++) {
            QDomElement elemTimer = document.createElement("Computer");
            elemTimer.setAttribute("cid", mComputers[i].mCId);
            elemTimer.setAttribute("bid", mComputers[i].mBId);
            elemTimer.setAttribute("rid", mComputers[i].mRId);

            domElem = document.createElement("name");
            domText = document.createTextNode(mComputers[i].mName);
            domElem.appendChild(domText);
            elemTimer.appendChild(domElem);

            domElem = document.createElement("account");
            domText = document.createTextNode(mComputers[i].mAccount);
            domElem.appendChild(domText);
            elemTimer.appendChild(domElem);

            domElem = document.createElement("password");
            domText = document.createTextNode(mComputers[i].mPassword);
            domElem.appendChild(domText);
            elemTimer.appendChild(domElem);

            elemAction.appendChild(elemTimer);
        }
    }

    document.appendChild(elemAction);


    if (QFile::exists(fileName)) {
        QString format("yyyyMMdd-hhmmss");
        QString newName = fileName + "." + QDateTime::currentDateTime().toString(format);
        QFile::rename(fileName, newName);
    }
    QFile file(fileName);
    if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        QTextStream stream(&file);
        stream << document.toString().toUtf8();

        file.close();
    }


    return document.toString().toUtf8();
}

void Action::clear()
{
    mTimers.clear();
    mComputers.clear();
}
